package com.hzm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzm.dao.OrderDao;
import com.hzm.dao.PayLogDao;
import com.hzm.entity.Order;
import com.hzm.entity.PayLog;
import com.hzm.service.OrderService;
import com.hzm.utils.Result;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @program: qy137-java
 * @description:
 * @author: 皇子明
 * @create: 2021-10-19 11:26
 **/
@RestController
@RequestMapping("/order/order")
public class OrderController {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private PayLogDao payLogDao;
    @Autowired
    private OrderService orderService;
    @ApiOperation(value = "查看所有信息")
    @PostMapping("/getAllOrder/{page}/{limit}")
    public Result getAllOrder(@PathVariable("page") Integer page, @PathVariable("limit") Integer limit, @RequestBody Order order){
        Result allOrder = orderService.getAllOrder(page, limit, order);
        return allOrder;
    }
    @ApiOperation(value = "根据id进行两表联查")
    @PostMapping("/queryOrderAndPayLog/{id}")
    public Result queryOrderAndPayLog(@PathVariable("id") @RequestBody String id){
        Result result =orderService.queryOrderAndPayLog(id);
        return  result;
    }
    @ApiOperation(value = "根据课程id查询所有的订单信息")
    @GetMapping("/queryOrderByPay")
    public Result queryOrderByPay(){
        Result result = orderService.queryOrderByPay();
        return result;
    }
    @ApiOperation(value = "根据课程id和会员id查询支付状态")
    @PostMapping("/getOrderStatus/{id}/{HuiYuanId}")
    public Result getOrderStatus(@PathVariable("id") String id,@PathVariable("HuiYuanId") String HuiYuanId){
        Result result = orderService.getOrderStatusByCourseIdAndHuiYuanId(id,HuiYuanId);
        return  result;
    }
    @ApiOperation(value = "更改支付宝状态")
    @PostMapping("/updateZhiFuStatus/{id}/{HuiYuanId}")
    public Result updateZhiFuStatus(@PathVariable("id") String id,@PathVariable("HuiYuanId") String HuiYuanId){
        Result result = orderService.updateZhifuStatus(id,HuiYuanId);
        return result;

    }
    @ApiOperation(value = "更改支付宝状态")
    @GetMapping("/updateStatus/{order_no}")
    public Result updateStatus(@PathVariable String order_no, HttpServletResponse response)throws IOException {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no",order_no);
        Order order = orderDao.selectOne(wrapper);
        Result result= orderService.updateStatus(order_no);
        QueryWrapper<PayLog> queryWrapper = new QueryWrapper<>();
        PayLog payLog = new PayLog();
        payLog.setTradeState("SUCCESS");
        wrapper.eq("order_no",order);
        payLogDao.update(payLog,queryWrapper);
        response.sendRedirect("http://192.168.31.120:8081/DetailId/"+order.getCourseId());
        return result;
    }
    @ApiOperation(value = "更改支付宝状态")
    @GetMapping("/updateZFBStatus/{order_no}")
    public Result updateZFBStatus(@PathVariable String order_no, HttpServletResponse response) throws IOException {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no",order_no);
        Order order = orderDao.selectOne(wrapper);
        Result result = orderService.updateStatus(order_no);
        QueryWrapper<PayLog> queryWrapper = new QueryWrapper<>();
        PayLog payLog = new PayLog();
        payLog.setTradeState("SUCCESS");
        wrapper.eq("order_no",order_no);
        payLogDao.update(payLog,queryWrapper);
        response.sendRedirect("http://192.168.31.120:8081/myOrder");
        return result;
    }
    @ApiOperation(value = "查询当前订单的信息")
    @PostMapping("/queryCurrentOrderXinXi/{id}/{HuiYuanId}")
    public Result queryCurrentOrderXinXi(@PathVariable("id") String id,@PathVariable("HuiYuanId") String HuiYuanId){
        Result result = orderService.queryCurrentOrderXinXi(id, HuiYuanId);
        return result;
    }

}













